package com.servlet.music;


import com.dao.*;
import com.dao.impl.*;
import com.entity.Banner;
import com.entity.Comment;
import com.entity.Song;
import com.entity.Songer;
import com.entity.vo.SongVo;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.servlet.teacher.BaseServlet;
import com.util.JsonUtil;
import com.util.PageUtil;
import com.util.ProxyUtil;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/SongServlet")
public class SongServlet extends BaseServlet {


	/**
	 * 添加歌曲
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void insert(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		GsonBuilder gsonBuilder = new GsonBuilder();
		Gson gson = gsonBuilder.create();
		PrintWriter out = resp.getWriter();
		JsonUtil jsonUtil = new JsonUtil();

		SongService songService = (SongService)ProxyUtil.getDaoProxy(new SongServiceImpl());

		try {
			Song song = new Song();
			BeanUtils.populate(song, req.getParameterMap());

			int insert = songService.insert(song);

		} catch (Exception e) {
			e.printStackTrace();
			jsonUtil.setCode(201);
			jsonUtil.setMsg(e.getMessage());
			out.print(gson.toJson(jsonUtil));
			return;
		}

		jsonUtil.setCode(200);
		out.print(gson.toJson(jsonUtil));
	}

	/**
	 * 添加一个歌手，只需要歌名添加其他默认
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void addSonger(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		GsonBuilder gsonBuilder = new GsonBuilder();
		Gson gson = gsonBuilder.create();
		PrintWriter out = resp.getWriter();
		JsonUtil jsonUtil = new JsonUtil();

		SongService songService = (SongService)ProxyUtil.getDaoProxy(new SongServiceImpl());
		int songId = 0;
		try {
			String songName = req.getParameter("name");
			if(songName == null || songName.length() == 0) throw new RuntimeException("请输入歌手名字");

			songId = songService.addSonger(songName);

		} catch (Exception e) {
			e.printStackTrace();
			jsonUtil.setCode(201);
			jsonUtil.setMsg(e.getMessage());
			out.print(gson.toJson(jsonUtil));
			return;
		}
		jsonUtil.setData(songId);
		jsonUtil.setCode(200);
		out.print(gson.toJson(jsonUtil));
	}

	/**
	 * 更新歌曲
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		GsonBuilder gsonBuilder = new GsonBuilder();
		Gson gson = gsonBuilder.create();
		PrintWriter out = resp.getWriter();
		JsonUtil jsonUtil = new JsonUtil();

		SongService songService = (SongService)ProxyUtil.getDaoProxy(new SongServiceImpl());

		try {
			Song song = new Song();
			BeanUtils.populate(song, req.getParameterMap());

			int update = songService.update(song);

		} catch (Exception e) {
			e.printStackTrace();
			jsonUtil.setCode(201);
			jsonUtil.setMsg(e.getMessage());
			out.print(gson.toJson(jsonUtil));
			return;
		}

		jsonUtil.setCode(200);
		out.print(gson.toJson(jsonUtil));
	}

	/**
	 * 根据id查询某一个歌手
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		GsonBuilder gsonBuilder = new GsonBuilder();
		Gson gson = gsonBuilder.create();
		PrintWriter out = resp.getWriter();
		JsonUtil jsonUtil = new JsonUtil();

		String id = req.getParameter("id");

		SongerService songerService = (SongerService) ProxyUtil.getDaoProxy(new SongerServiceImpl());
		Song song =  null;
		try {
			Integer iId = Integer.parseInt(id);

			song =  songerService.queryDataById(iId);

		} catch (Exception e) {
			e.printStackTrace();
			jsonUtil.setCode(201);
			jsonUtil.setMsg(e.getMessage());
			out.print(gson.toJson(jsonUtil));
			return;
		}

		jsonUtil.setCode(200);
		jsonUtil.setData(song);
		out.print(gson.toJson(jsonUtil));
	}

	/**
	 *  查询所有未删除的歌手
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void queryDataSonger(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		GsonBuilder gsonBuilder = new GsonBuilder();
		Gson gson = gsonBuilder.create();
		PrintWriter out = resp.getWriter();
		JsonUtil jsonUtil = new JsonUtil();

		SongerService songerService = (SongerService) ProxyUtil.getDaoProxy(new SongerServiceImpl());
		List<Songer> list = null;
		try {

			list =  songerService.queryListSong();

		} catch (Exception e) {
			e.printStackTrace();
			jsonUtil.setCode(201);
			jsonUtil.setMsg(e.getMessage());
			out.print(gson.toJson(jsonUtil));
			return;
		}

		jsonUtil.setCode(200);
		jsonUtil.setData(list);
		out.print(gson.toJson(jsonUtil));
	}


	/**
	 * 分页查询歌曲
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void queryDataBlur(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		GsonBuilder gsonBuilder = new GsonBuilder();
		Gson gson = gsonBuilder.create();
		PrintWriter out = resp.getWriter();
		JsonUtil jsonUtil = new JsonUtil();

		SongService songService = (SongService)ProxyUtil.getDaoProxy(new SongServiceImpl());

		PageUtil<SongVo> songPageUtil = null;

		try {

			String blur = req.getParameter("blur");

			songPageUtil = songService.queryDataBlur(Integer.parseInt(req.getParameter("pageNum")),
					Integer.parseInt(req.getParameter("pageSize")), blur);
		} catch (Exception e) {
			e.printStackTrace();
			jsonUtil.setCode(201);
			jsonUtil.setMsg(e.getMessage());
			out.print(gson.toJson(jsonUtil));
			return;
		}

		jsonUtil.setCode(200);
		jsonUtil.setData(songPageUtil);
		out.print(gson.toJson(jsonUtil));
	}

	/**
	 * 删除歌曲
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void deleteById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		GsonBuilder gsonBuilder = new GsonBuilder();
		Gson gson = gsonBuilder.create();
		PrintWriter out = resp.getWriter();
		JsonUtil jsonUtil = new JsonUtil();

		SongService songService = (SongService)ProxyUtil.getDaoProxy(new SongServiceImpl());
		int res = 0;
		try {
			String songId = req.getParameter("id");
			if(songId == null || songId.length() == 0) throw new RuntimeException("请选择删除的风格");

			res = songService.deleteById(songId);

		} catch (Exception e) {
			e.printStackTrace();
			jsonUtil.setCode(201);
			jsonUtil.setMsg(e.getMessage());
			out.print(gson.toJson(jsonUtil));
			return;
		}
//		jsonUtil.setData(styleId);
		jsonUtil.setCode(200);
		out.print(gson.toJson(jsonUtil));
	}

}
